package com.antwork.web.action;

import com.antwork.common.dao.DynamicSqlParameter;
import com.antwork.common.entity.Pagination;
import com.antwork.service.UserService;
import com.antwork.web.common.BaseAction;
import java.net.URLDecoder;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Scope("prototype")
@Controller
@Namespace("/user")
@ParentPackage("json-default")
public class UserAction extends BaseAction {
	private static final long serialVersionUID = -404931035168989355L;

	@Autowired
	private UserService userService;

	@Action(value = "list", results = {
			@org.apache.struts2.convention.annotation.Result(name = "all", type = "dispatcher", location = "/user/user_list.jsp"),
			@org.apache.struts2.convention.annotation.Result(name = "male", type = "dispatcher", location = "/user/user_list_male.jsp"),
			@org.apache.struts2.convention.annotation.Result(name = "female", type = "dispatcher", location = "/user/user_list_female.jsp") })
	public String list() throws Exception {
		String tab = getParameterOfTab("all");
		DynamicSqlParameter param = new DynamicSqlParameter();
		param.setPagination(getParameterOfPage(), 10);
		if (tab.equals("all")) {
			String breadcrumb = getParameterOfBreadcrumb("all");
			if (breadcrumb.equals("male"))
				param.put("sex", "1");
			else if (breadcrumb.equals("female")) {
				param.put("sex", "0");
			}
			Pagination pagination = this.userService.getUserList(param);
			setOutputOfPagination(pagination);
		} else if (tab.equals("male")) {
			param.put("sex", Integer.valueOf(1));
			String breadcrumb = getParameterOfBreadcrumb("hasmobile");
			if (breadcrumb.equals("hasmobile"))
				param.put("hasmobile", Integer.valueOf(1));
			else {
				param.put("nomobile", Integer.valueOf(1));
			}
			Pagination pagination = this.userService.getUserList(param);
			setOutputOfPagination(pagination);
		} else if (tab.equals("female")) {
			param.put("sex", Integer.valueOf(0));
			String queryType = getParameterOfQueryType();
			if (queryType != null) {
				String query = getParameterOfQuery();
				if (queryType.equals("id")) {
					if (query != null) {
						param.put("userid", query);
					}
				} else if (query != null) {
					query = URLDecoder.decode(query, "utf-8");
					param.put("username", query);
				}

			}

			Pagination pagination = this.userService.getUserList(param);
			setOutputOfPagination(pagination);
		}
		return tab;
	}
}